package cn.lopr.im.android.mvp.view.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.ArticlePlate;

public class CommunityPlateAdapter extends RecyclerView.Adapter<CommunityPlateAdapter.ViewHolder> {

    private List<ArticlePlate> articlePlates;
    private final Context context;
    private CommunityPlateAdapter.OnRecyclerViewItemClickListener mOnItemClickListener;

    public CommunityPlateAdapter(Context context, List<ArticlePlate> articlePlates) {
        this.articlePlates = articlePlates;
        this.context = context;
    }

    public void refresh(List<ArticlePlate> articlePlates) {
        this.articlePlates = articlePlates;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_community_plate, parent, false);

        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ArticlePlate articlePlate = this.articlePlates.get(position);
        holder.mView.setTag(position);
        holder.tvCommunityPlate.setText(articlePlate.getName());
        if (articlePlate.isCurrent()) {
            holder.tvCommunityPlate.setTextColor(Color.BLACK);
            holder.tvCommunityPlate.setTextSize(18);
            holder.v.setVisibility(View.VISIBLE);
        } else {
            holder.tvCommunityPlate.setTextColor(Color.GRAY);
            holder.tvCommunityPlate.setTextSize(16);
            holder.v.setVisibility(View.GONE);
        }
        if (this.mOnItemClickListener != null) {
            holder.tvCommunityPlate.setTag(articlePlate);
            holder.tvCommunityPlate.setOnClickListener(e -> mOnItemClickListener.onItemClick(e, position));
        }
    }

    @Override
    public int getItemCount() {
        return articlePlates == null ? 0 : articlePlates.size();
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int section);

        void onItemLongClick(View view, int section);
    }

    public void onRecyclerViewItemClickListener(CommunityPlateAdapter.OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View mView;

        TextView tvCommunityPlate;
        View v;

        ViewHolder(View view) {
            super(view);
            this.mView = view;
            this.tvCommunityPlate = view.findViewById(R.id.tv_community_plate);
            this.v = view.findViewById(R.id.v);
        }

    }
}
